﻿Imports System.Net
Imports System.Diagnostics.Process

Imports System.IO

Public Class FTP
    Public Property UserName As String
    Public Property Server_Ip As System.Net.IPAddress
    Public Property Carpeta As String
    Private Proceso As Process

    Public Event SinConexion(ByVal sender As Object, ByVal e As ConexionEvenArgs)

    Public Sub New(ByVal user As String, ByVal Ip As String)
        Me.UserName = user

        Me.Server_Ip = New System.Net.IPAddress(IPAddress.Parse(Ip).GetAddressBytes)


    End Sub
    Public Sub New(ByVal Ip As String)
        Me.Server_Ip = New System.Net.IPAddress(IPAddress.Parse(Ip).GetAddressBytes)
        Me.UserName = "abcd"
    End Sub
    Public Sub Preparacion()
        Dim compresor As New Process

        Dim archivo As String = Carpeta.Split("\").GetValue(Carpeta.Split("\").Length - 1)
        ' Dim ruta As String = Path.GetFullPath(My.Resources.ResourceManager.BaseName.Replace(".", "\"))
        compresor.StartInfo = New ProcessStartInfo(My.Application.Info.DirectoryPath & "\winrar.exe")
        With compresor.StartInfo
            .Arguments = "-m0 a subida.rar " & archivo
            .CreateNoWindow = True
            .UseShellExecute = False
            .RedirectStandardInput = True
            .WindowStyle = ProcessWindowStyle.Hidden
            .RedirectStandardOutput = True
            .WorkingDirectory = My.Computer.FileSystem.GetParentPath(Carpeta)
            'Path.GetFullPath(My.Resources.ResourceManager.BaseName)
        End With
        Try
            compresor.Start()
        Catch ex As Exception
            RaiseEvent SinConexion(Me, e:=New ConexionEvenArgs(ex.Message))
        End Try
        Dim cont As Integer = 0
        While Not compresor.HasExited
            Form1.pbPreparacion.Increment(cont)
            cont += 10
            If cont >= 90 Then
                cont = 90
            End If
        End While
        Form1.pbPreparacion.Increment(10)
    End Sub
    Public Sub Conectar()
        My.Computer.FileSystem.CopyFile(My.Computer.FileSystem.GetParentPath(Carpeta) & "\subida.rar", System.IO.Path.GetTempPath & "\subida.rar", True)
        My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.GetParentPath(Carpeta) & "\subida.rar")
        'My.Computer.FileSystem.DeleteFile(Carpeta & "\subida.rar")

        Carpeta = System.IO.Path.GetTempPath & "\subida.rar"

        Proceso = New Process()
        Proceso.StartInfo = New ProcessStartInfo("ftp")
        With Proceso.StartInfo
            .Arguments = "-i"
            .CreateNoWindow = True
            .WindowStyle = ProcessWindowStyle.Hidden
            .UseShellExecute = False
            .RedirectStandardInput = True
            .RedirectStandardOutput = True
        End With
        Try
            Proceso.Start()
        Catch ex As Exception
            RaiseEvent SinConexion(Me, e:=New ConexionEvenArgs(ex.Message))
            Exit Sub
        End Try
        Me.Upload()

    End Sub

    Private Overloads Sub Upload()


        Try
            Dim writer As System.IO.StreamWriter = Proceso.StandardInput
            writer.WriteLine("open " & Server_Ip.ToString)
            writer.WriteLine(UserName)
            writer.WriteLine("123456")

            writer.WriteLine("mput " & Carpeta)
            'writer.WriteLine("y")

            writer.WriteLine("mkdir finish-it")
            writer.Close()
            Proceso.Close()

        Catch ex As Exception
            RaiseEvent SinConexion(Me, e:=New ConexionEvenArgs(ex.Message))
            Exit Sub

        End Try



    End Sub

    Public Sub Download()
        '"descarga.rar"
        Proceso = New Process()
        Proceso.StartInfo = New ProcessStartInfo("ftp")
        With Proceso.StartInfo
            .Arguments = "-i"
            .CreateNoWindow = True
            .WindowStyle = ProcessWindowStyle.Hidden
            .UseShellExecute = False
            .RedirectStandardInput = True
            .RedirectStandardOutput = True
        End With
        Try
            Proceso.Start()
        Catch ex As Exception
            RaiseEvent SinConexion(Me, e:=New ConexionEvenArgs(ex.Message))
            Exit Sub
        End Try

        Try
            Dim writer As System.IO.StreamWriter = Proceso.StandardInput
            writer.WriteLine("open " & Server_Ip.ToString)
            writer.WriteLine(UserName)
            writer.WriteLine("123456")

            writer.WriteLine("get " & "descargar.rar")
            'writer.WriteLine("y")


            writer.Close()
            Proceso.Close()
        Catch ex As Exception
            RaiseEvent SinConexion(Me, e:=New ConexionEvenArgs(ex.Message))
            Exit Sub

        End Try


    End Sub
End Class
